package com.zlzlib.librxoknet.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * @Desc:
 * @DateTime: 2021/2/23 9:03
 * @Author zlz
 * @Version 1.0
 */
public final class FastConverterFactory extends Converter.Factory {

    private volatile static FastConverterFactory instance = null;
    private SerializeConfig serializeConfig;
    private SerializerFeature[] serializerFeatures;
    private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
    private Feature[] features;

    private ParserConfig parserConfig = ParserConfig.getGlobalInstance();

    public static FastConverterFactory getInstance() {
        if (instance == null) {
            synchronized (FastConverterFactory.class) {
                if (instance == null) {
                    instance = new FastConverterFactory();
                }
            }
        }
        return instance;
    }

    private FastConverterFactory() {
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new FastResponseBodyConverter<>(type, parserConfig, featureValues, features);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new FastRequestBodyConverter<>(serializeConfig, serializerFeatures);
    }

    public SerializeConfig getSerializeConfig() {
        return serializeConfig;
    }

    public void setSerializeConfig(SerializeConfig serializeConfig) {
        this.serializeConfig = serializeConfig;
    }

    public SerializerFeature[] getSerializerFeatures() {
        return serializerFeatures;
    }

    public void setSerializerFeatures(SerializerFeature[] serializerFeatures) {
        this.serializerFeatures = serializerFeatures;
    }

    public int getFeatureValues() {
        return featureValues;
    }

    public void setFeatureValues(int featureValues) {
        this.featureValues = featureValues;
    }

    public Feature[] getFeatures() {
        return features;
    }

    public void setFeatures(Feature[] features) {
        this.features = features;
    }

    public ParserConfig getParserConfig() {
        return parserConfig;
    }

    public void setParserConfig(ParserConfig parserConfig) {
        this.parserConfig = parserConfig;
    }
}
